package cn.xiaopeng.mcp.server.weixin.infrastructure.adapter;

import cn.xiaopeng.mcp.server.weixin.domain.adapter.IWeixinPort;
import cn.xiaopeng.mcp.server.weixin.domain.model.WeixinSendMessageRequest;
import cn.xiaopeng.mcp.server.weixin.domain.model.WeixinSendMessageResponse;
import cn.xiaopeng.mcp.server.weixin.infrastructure.gateway.IWeixinApiService;
import cn.xiaopeng.mcp.server.weixin.infrastructure.gateway.dto.WeixinTemplateMessageDTO;
import cn.xiaopeng.mcp.server.weixin.infrastructure.gateway.dto.WeixinTokenResponseDTO;
import cn.xiaopeng.mcp.server.weixin.type.WeixinProperties;
import com.alibaba.fastjson.JSONObject;
import com.google.common.cache.Cache;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import retrofit2.Call;
import retrofit2.Response;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jiangyangang
 */
@Component
@Slf4j
public class WeixinPort implements IWeixinPort {

    @Resource
    private IWeixinApiService  weixinApiService;

    @Resource
    private WeixinProperties weixinProperties;

    @Resource
    private Cache<String, String> weixinAccessToken;


    @Override
    public WeixinSendMessageResponse sendMessage(WeixinSendMessageRequest request) throws IOException {

        log.info("请求推送微信通知, req:{}", JSONObject.toJSONString(request));

        String accessToken = weixinAccessToken.getIfPresent(weixinProperties.getAppid());
        if (StringUtils.isEmpty(accessToken)) {
            Call<WeixinTokenResponseDTO> clientCredential = weixinApiService.getToken("client_credential", weixinProperties.getAppid(), weixinProperties.getAppsecret());
            WeixinTokenResponseDTO response = clientCredential.execute().body();
            assert response != null;
            weixinAccessToken.put(weixinProperties.getAppid(), response.getAccess_token());
            accessToken = response.getAccess_token();
        }

        // 发送模板消息
        WeixinTemplateMessageDTO messageDTO = new WeixinTemplateMessageDTO(weixinProperties.getTouser(), weixinProperties.getTemplate_id());

        HashMap<String, Map<String, String>> data = new HashMap<>();
        WeixinTemplateMessageDTO.put(data, WeixinTemplateMessageDTO.TemplateKey.platform, request.getPlatform());
        WeixinTemplateMessageDTO.put(data, WeixinTemplateMessageDTO.TemplateKey.subject, request.getSubject());
        WeixinTemplateMessageDTO.put(data, WeixinTemplateMessageDTO.TemplateKey.description, request.getDescription());

        messageDTO.setData(data);
        messageDTO.setUrl(request.getJumpUrl());

        log.info("开始推送微信通知, req:{}", JSONObject.toJSONString(request));
        Response<Void> response = weixinApiService.sendMessage(accessToken, messageDTO).execute();
        WeixinSendMessageResponse messageResponse = new WeixinSendMessageResponse();
        messageResponse.setSuccess(response.isSuccessful());
        log.info("结束推送微信通知, resp:{}", response.isSuccessful());

        return messageResponse;
    }
}
